

package com.example.administrator.caigou51.recyclerCard.cardView;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.caigou51.EventBusClass.GotoFragment;
import com.example.administrator.caigou51.EventBusClass.UpdateCheckOrderActivity;
import com.example.administrator.caigou51.EventBusClass.UpdateGoodsListActivity;
import com.example.administrator.caigou51.EventBusClass.UpdateShopCarFragment;
import com.example.administrator.caigou51.EventBusClass.UpdateShopCarFragmentRemove;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.CheckOrderActivity;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.activity.GoodsListActivity;
import com.example.administrator.caigou51.activity.MainActivity;
import com.example.administrator.caigou51.bean.CheckOrderBean;
import com.example.administrator.caigou51.control.DialogGoodsBuy;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.CardItemView;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.GoodsListCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;
import com.example.administrator.caigou51.util.ImageUtil;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.widget.dialog.SweetDialogManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import de.greenrobot.event.EventBus;


public class GoodsListCardView extends CardItemView<GoodsListCard> {
 	private Context mContext;

    public GoodsListCardView(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public GoodsListCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public GoodsListCardView(Context context,
                                    AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
        // TODO Auto-generated constructor stub
    }
    private RelativeLayout relativeLayoutOneGoods,relativeLayoutGuangDong,relativeLayoutTips;
    private ImageView imageViewGoods,imageViewSellAll,imageViewdel;
    private TextView textViewGoodsName,textViewSellScopen;
    private TextView textViewMoneyIcon;
    private TextView textViewGoodsPrice;
    private TextView textViewGoodsCount;
    private List<CheckOrderBean.GoodsEntity> goodsEntityListNew;
    @Override
    public void build(final GoodsListCard card) {
        // TODO Auto-generated method stub
        super.build(card);
        relativeLayoutGuangDong= (RelativeLayout) findViewById(R.id.relativeLayoutGuangDong);
        relativeLayoutOneGoods = (RelativeLayout) findViewById(R.id.relativeLayoutOneGoods);
        relativeLayoutTips= (RelativeLayout) findViewById(R.id.relativeLayoutTips);
        imageViewdel= (ImageView) findViewById(R.id.imageViewdel);
        imageViewGoods = (ImageView) findViewById(R.id.imageViewGoods);
        imageViewSellAll= (ImageView) findViewById(R.id.imageViewSellAll);
        textViewGoodsName = (TextView) findViewById(R.id.textViewGoodsName);
        textViewMoneyIcon = (TextView) findViewById(R.id.textViewMoneyIcon);
        textViewGoodsPrice = (TextView) findViewById(R.id.textViewGoodsPrice);
        textViewGoodsCount = (TextView) findViewById(R.id.textViewGoodsCount);
        textViewSellScopen = (TextView) findViewById(R.id.textViewSellScopen);
        App.displayImageHttpOrFile(card.getGoodsEntity().getThumb(),imageViewGoods, ImageUtil.OptionsActivity());
        textViewGoodsName.setText(card.getGoodsEntity().getTitle()+card.getGoodsEntity().getProduct_spec());
        textViewGoodsPrice.setText(card.getGoodsEntity().getShip_price());
        textViewGoodsCount.setText("数量:"+card.getGoodsEntity().getNumber());

        //(1代表全国，2代表限广东）
        if ("2".equals(card.getGoodsEntity().getSale_range())) {
            relativeLayoutGuangDong.setVisibility(View.VISIBLE);
            imageViewSellAll.setImageResource(R.drawable.icon_sell_gd);
            textViewSellScopen.setText("销售范围:广东省");
            textViewSellScopen.setTextColor(mContext.getResources().getColor(R.color.red));
            imageViewdel.setVisibility(View.VISIBLE);
            imageViewdel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //判断是否从购物车进来，是就要删除购物车的商品

                        SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity)mContext, "           是否要删除\n只限广东地区批发商品", "", false, false, new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismiss();
                            }
                        }, new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                taskDelCar((GBaseActivity) mContext,null,getCheckIDS(card.getGoodsEntityList()),card);


                            }
                        });


                }
            });
        }else
        {
            relativeLayoutGuangDong.setVisibility(View.GONE);
            imageViewSellAll.setImageResource(R.drawable.icon_sell_all);
            textViewSellScopen.setText("销售范围:全国");
            textViewSellScopen.setTextColor(mContext.getResources().getColor(R.color.color_1ca146));
            imageViewdel.setVisibility(View.GONE);
        }


        if (card.isEnd())
        {
            //判断是否含有广东的商品
            boolean havaGuangDongGoods=false;
            for (int i=0;i<card.getGoodsEntityList().size();i++)
            {
                //(1代表全国，2代表限广东）
                if ("2".equals(card.getGoodsEntityList().get(i).getSale_range())) {
                    havaGuangDongGoods = true;
                    break;
                }
            }
            if (havaGuangDongGoods)
            relativeLayoutTips.setVisibility(View.VISIBLE);
            else
                relativeLayoutTips.setVisibility(View.GONE);
        }else
        {
            relativeLayoutTips.setVisibility(View.GONE);
        }
    }
    private String getCheckIDS(List<CheckOrderBean.GoodsEntity> goodsEntityList) {
        goodsEntityListNew=new ArrayList<>();
        String ids="";

        for (int i=0;i<goodsEntityList.size();i++)
        {

            if (goodsEntityList.get(i).getSale_range().contains("2")) {
                ids += goodsEntityList.get(i).getItemid() + ",";

            }else
            {
                goodsEntityListNew.add(goodsEntityList.get(i));
            }

        }
        if (!TextUtils.isEmpty(ids)) {
            ids = ids.substring(0, ids.length() - 1);
        }
        return ids;
    }
    private String getCheckIDSQuanGuo(List<CheckOrderBean.GoodsEntity> goodsEntityList) {
        String ids="";

        for (int i=0;i<goodsEntityList.size();i++)
        {

            if (!goodsEntityList.get(i).getSale_range().contains("2")) {
                ids += goodsEntityList.get(i).getItemid() + ",";

            }

        }
        if (!TextUtils.isEmpty(ids)) {
            ids = ids.substring(0, ids.length() - 1);
        }
        return ids;
    }

    public  void taskDelCar(final GBaseActivity mContext, MaterialListView materialListView, final String ids, final GoodsListCard card) {


        RequestServerManager.getInstanceDIY().handleMethodDIY(mContext, materialListView, false, null,
                Constant.getRootUrl() + Constant.Action.Action_DelShopCar, Constant.Action.Action_DelShopCar, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_DelShopCar,App.getUserBean().getUserid(),App.getUserBean().getSession_id(),ids), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();


                        DialogGoodsBuy.TaskRefreshSaveShopCarData(false);
                        //刷新当前页面
                        EventBus.getDefault().post(new UpdateGoodsListActivity(goodsEntityListNew));
                        if (goodsEntityListNew.size()>0) {
                            //刷新订单页面
                            EventBus.getDefault().post(new UpdateCheckOrderActivity(getCheckIDSQuanGuo(card.getGoodsEntityList())));
                            EventBus.getDefault().post(new UpdateShopCarFragmentRemove(ids));
                            ToastUtil.showToast("操作成功");
                        }else
                        {
                            ToastUtil.showToast("操作成功,订单暂无商品");
                        }
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        ToastUtil.showToast("删除失败");
                    }
                });
    }
}
